import numpy as np, scipy.special as sp, scipy.integrate as ig, copy, re, math
import sys
from Model import Model,Actor,Evidence,TrackSegment,Node,TimeNode,Time,Distributions

class Loiter:

    def __init__(self):
        self.model = Model()
        self.model.name = "Loiter"
        self.model.subscriptions = [""]
        self.model.topNode = "loitermain"
        self.model.independent = True

        act1 = Actor()
        act1.name = "dummy1"
        act1.atype = Actor.NULL

        tempNode = Node()
        tempNode.getID()
        tempNode.name = "loitermain"
        tempNode.subjects = [act1]
        tempNode.objects = []
        tempNode.evvecs = []
        tempNode.bound = False

        self.model.topNode = tempNode.name
        self.model.nodes.append(copy.deepcopy(tempNode))

        tempNode.name = "loitersub"
        tempEv = TrackSegment()
        tempEv.actor = act1
        tempEv.segtype = Evidence.MOVE
        tempEv.willBind = True
        tempEv.endsNode = True
        tempEv.bound = False

        tempNode.evvecs = [copy.deepcopy(tempEv)]

        def loiter_joint(evvecs):
            track = evvecs[0]
            average_distance_from_center = (
                track.calculate_distance_from_center())
            # print ("calculated distance from center:%s" %
            #        average_distance_from_center)
            distanceWeight = Loiter.average_distance_density(
                average_distance_from_center)
            # print "weight:",distanceWeight
            time = (track.endtime - track.starttime)/Model.FR/60.
            # print "time:",time
            timeWeight = Loiter.time_density(time)
            # print "time weight:",timeWeight
            totalWeight = timeWeight + distanceWeight
            return totalWeight,.5
            # return avgDistanceWeight,.5

        tempNode.lfunction = loiter_joint

        self.model.nodes.append(copy.deepcopy(tempNode))

    @staticmethod
    def average_distance_density(avgDistance):
        weight = Distributions.PDFs.foldednorm(avgDistance, 0.5, 3.)
        return weight

    @staticmethod
    def time_density(time):
        #loitering time is in minutes
        weight = Distributions.PDFs.gamma(time,0.5,.8)
        return weight
